package com.cskaoyan.controller.mall;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.PageDataVo;
import com.cskaoyan.bean.mall.brand.BrandInfoListVO;
import com.cskaoyan.bean.mall.order.PageInfoBO;
import com.cskaoyan.bean.mall.brand.Brand;
import com.cskaoyan.service.mall.BrandService;
import com.cskaoyan.utils.Constant;
import com.cskaoyan.utils.ValidationUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@RestController
@RequestMapping("/admin/brand")
public class BrandController {
    @Autowired
    BrandService BrandService;

    @RequiresPermissions(" admin:brand:list")
    @GetMapping("list")
    public BaseRespVo queryBrandInfoList(PageInfoBO pageInfoBO, String id, String name) {
        Integer integerId=null;
        try {
            if (id!=null&&!"".equals(id)) integerId = Integer.valueOf(id);
        } catch (Exception e) {
            return BaseRespVo.error("请输入数字id");
        }
        PageDataVo listVO = BrandService.queryBrandInfoList(pageInfoBO, integerId, name);
        if (listVO == null) BaseRespVo.error();
        return BaseRespVo.ok(listVO);
    }

    @RequiresPermissions("admin:brand:update")
    @PostMapping("update")
    public BaseRespVo updateBrandInfo(@RequestBody @Validated Brand brand, BindingResult result) {
        ValidationUtil.valid(result);
        Brand brandInfo = BrandService.updateBrandInfo(brand);
        if (brandInfo != null) return BaseRespVo.ok(brandInfo);
        return BaseRespVo.error();
    }

    @RequiresPermissions("admin:brand:delete")
    @PostMapping("delete")
    public BaseRespVo deleteBrandInfo(@RequestBody @Validated Brand brand, BindingResult result) {
        ValidationUtil.valid(result);
        int code = BrandService.deleteBrandInfo(brand);
        if (code == Constant.SUCCESS) return BaseRespVo.ok();
        return BaseRespVo.error("删除失败");
    }

    @RequiresPermissions("admin:brand:create")
    @PostMapping("create")
    public BaseRespVo createBrandInfo(@RequestBody @Validated Brand brand, BindingResult bindingResult) throws IOException {
        Brand brandInfo = BrandService.createBrandInfo(brand);
        if (bindingResult == null) return BaseRespVo.error("添加失败");
        return BaseRespVo.ok(brandInfo);
    }
}
